<?php
namespace app\admin\controller;

//标签管理
class Label extends Common
{
	//标签列表
    public function label_list()
    {
		$where=array('a.gl_id'=>array('gt',0));
        $name=Request()->param('name');
		//名称
		$status=Request()->param('status');
		//状态
        $start_time=strtotime(Request()->param('start_time'));
        $end_time=strtotime(Request()->param('end_time'));
        $page=Request()->param('page');
        $size=Request()->param('size');
        if($page<1){
            $page=1;
        }
        if($size<15){
            $size=15;
        }
        $now=($page-1)*$size;
		if($name!=''){
			$where['a.gl_name']=array('like','%'.$name.'%');
		}
		if($status>0){
			if($status==1){
				$where['a.gl_status']=1;
			}else{
				$where['a.gl_status']=2;
			}
			
		}
        $where_time=array(array('gt',0),array('gt',0));
        if($start_time>0){
            $where_time[]=array('egt',$start_time);
        }else{
            $start_time='';
        }
        if($end_time>0){
            $where_time[]=array('elt',$end_time);
        }else{
            $end_time='';
        }
        $where['a.gl_create_time']=$where_time;
        $label=db('goods_label a')
            ->field("a.*")
			->order('a.gl_sort desc,a.gl_status')
			->where($where)
            ->limit("$now,$size")
			->select();
        foreach($label as $k=>$v){
            $label[$k]['g_number']=db('goods')
                ->where(array('gl_ids'=>array('like','%,'.$v['gl_id'].',%')))
                ->count();
        }
		//标签列表
        $count=db('goods_label a')
            ->where($where)
            ->count();
        //等级信息
        $page_str=page($page,$size,$count,count($label));//分页
        $this->view->assign('page_str',$page_str);
		$this->view->assign('data',$label);
		$this->assign('status',$status);
		$this->assign('name',$name);
        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
        $this->assign('page',$page);
        $this->assign('size',$size);
        $this->assign('count',$count);
        return $this->view->fetch();

    }
	//添加标签
	public function add_label(){
		if(request()->isGet()){
			//get，加载页面
			return $this->view->fetch();
		}else if(request()->isAjax()){
			$data=request()->post();
            $data['gl_name']=trim($data['gl_name']);
			if($data['gl_name']==''){
                return return_json(array('code'=>0,'msg'=>'请输入标签名称'),'添加标签');
            }
            if($data['gl_sort']<=0){
                $data['gl_sort']=0;
            }
            $label=db('goods_label')->where(array('gl_name'=>$data['gl_name']))->find();
            if(!empty($label)){
                return return_json(array('code'=>0,'msg'=>'已有相同名称的标签'),'添加标签');
            }
            if($data['gl_status']!=1){
                $data['gl_end_time']=time();
            }
            $data['gl_create_time']=time();
            $data['gl_add_user_id']=session('uid');
            $data['gl_add_user_name']=session('user.real_name');
            $res=db('goods_label')->insertGetId($data);
            //添加权限
            if($res>0){
                return return_json(array('code'=>1,'msg'=>'标签添加成功'),'添加标签');
            }else{
                return return_json(array('code'=>0,'msg'=>'标签添加失败'),'添加标签');
            }
		}
    }
    //修改标签
    public function relabel(){
        if(request()->isGet()){
            //get，加载页面
            $id=request()->get('id');
            if($id==1 || $id==2){
                return '<h1>该标签为不可更改标签</h1>';
            }
            $label=db('goods_label')
                ->where(array('gl_id'=>$id))
                ->find();
            $this->assign('label',$label);
            return $this->view->fetch();
        }else if(request()->isAjax()){
            $data=request()->post();
            $id=$data['id'];
            unset($data['id']);
            $data['gl_name']=trim($data['gl_name']);
            if($data['gl_name']==''){
                return return_json(array('code'=>0,'msg'=>'请输入标签名称'),'修改标签');
            }
            if($data['gl_sort']<=0){
                $data['gl_sort']=0;
            }
            $label=db('goods_label')->where(array('gl_name'=>$data['gl_name'],'gl_id'=>array('neq',$id)))->find();
            if(!empty($label)){
                return return_json(array('code'=>0,'msg'=>'已有相同名称的标签'),'修改标签');
            }
            if($data['gl_status']!=1){
                $label=db('goods_label')->where(array('gl_id'=>$id))->find();
                if($label['gl_status']==1){
                    $data['gl_end_time']=time();
                }
            }
            $res=db('goods_label')->insertGetId($data);
            //添加权限
            if($res>0){
                return return_json(array('code'=>1,'msg'=>'标签修改成功'),'修改标签');
            }else{
                return return_json(array('code'=>0,'msg'=>'标签修改失败'),'修改标签');
            }
        }
    }
    //禁用标签
    function label_del(){
        if(request()->isAjax()){
            $id=request()->post('id');
            if($id==1 || $id==2){
                return return_json(array('code'=>0,'msg'=>'该标签为不可更改标签'),'禁用标签');
            }
            $res=db('goods_label')
                ->where(array('gl_id'=>$id))
                ->update(array('gl_status'=>2,'gl_end_time'=>time()));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'禁用成功'),'禁用标签');
            }else{
                return return_json(array('code'=>0,'msg'=>'禁用失败'),'禁用标签');
            }
        }
    }
    //启用标签
    function label_use(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $res=db('goods_label')
                ->where(array('gl_id'=>$id))
                ->update(array('gl_status'=>1,'gl_end_time'=>0));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'启用成功'),'启用标签');
            }else{
                return return_json(array('code'=>0,'msg'=>'启用失败'),'启用标签');
            }
        }
    }
}
